import baseInfo from "./baseInfo";
import order from "./order";
import draw from "./draw";
import merchant from "./merchant";
// import stock from "./stock";
import system from "./system";
import transfer from "./transfer";
import { user } from "./user";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import voucher from "@/router/voucher";
export const
  baseName = "分销系统-管理端",
  routes: gt.RouterType[] = [
    { path: "/", name: "/", component: () => import("@/views/index.vue"), meta: { title: "首页", per: 1 } },
    { path: "/login", name: "/login", component: () => import("@/views/login.vue"), meta: { title: "登录", per: 1, notLayout: true } },
    { path: "/404", name: "/404", component: () => import("@/views/404.vue"), meta: { title: "页面不存在", per: 1, notLayout: true } },
    { path: "/:pathMatch(.*)*", name: "404", component: () => import("@/views/404.vue"), meta: { title: "页面不存在", per: 1, notLayout: true } },
  ],
  allRoutes = [baseInfo, order, user, merchant, draw, voucher, system, transfer,],
  whiteList = ["/404", "/login"];
function formatRoutes(rs: gt.RouterType[], p = "/") {
  for (const e of rs) {
    if (e.path.indexOf(p)) {
      e.path = `${p}/${e.path}`;
    }
    while ((e.name = e.path.replaceAll("//", "/")) != e.path) {
      e.path = e.name;
    }
    e.meta = Object.assign({}, e.meta, { per: 1 });
    if (Array.isArray(e.children)) {
      formatRoutes(e.children, e.path);
    }
  }
  return rs;
}
routes.push(...formatRoutes(allRoutes as any));
const router = createRouter({ history: createWebHashHistory(), routes: routes as RouteRecordRaw[] });
let isSetPer = 100;
async function setPer(p: obj[] = allRoutes, exe?: any) {
  if (Array.isArray(p)) {
    if (!exe) {
      while (api.token && !api.mid && --isSetPer > 0) {
        await next();
      }
      if (!api.mid) { return; }
    }
    isSetPer = 100;
    for (let i = 0; i < p.length; i++) {
      const e = p[i];
      Array.isArray(e.children) && await setPer(e.children, true);
      if (e.meta) {
        e.meta.per = !!e.meta.per || api.me.per(e.meta.ps);
        if (!e.meta.per && Array.isArray(e.children) && e.children.length) {
          e.meta.per = true;
        }
      }
    }
  } else {
    isSetPer = 0;
  }
  return p;
}
router.beforeEach(async (to, _from, _next) => {
  if (whiteList.includes(to.path)) {
    _next();
  } else {
    await setPer();
    if (api.token) {
      let title = baseName;
      to.matched.forEach(e => { title += ">" + e.meta.title; });
      document.title = title;
      if (to.meta.per) {
        _next();
      } else {
        _next("/404");
      }
    } else {
      _next("/login");
    }
  }
});
function getTreeElMenuData(p: gt.RouterType[]) {
  const ret: gt.components.TreeElMenuData[] = [];
  for (const v of p) {
    const { path, meta, children, opt } = v;
    if (meta && meta.per) {
      ret.push({
        name: meta.title,
        opt: Object.assign(opt || {}, { index: path }),
        children: children?.length ? getTreeElMenuData(children) : undefined
      });
    }
  }
  return ret;
}
export async function getMenu() {
  if (api.mid) {
    let i = -1;
    if (!api.s.distribution) {
      i = allRoutes.indexOf(merchant);
      if (i >= 0) { allRoutes.splice(i, 1); }
      i = allRoutes.indexOf(draw);
      if (i >= 0) { allRoutes.splice(i, 1); }
      i = allRoutes.indexOf(transfer);
      if (i >= 0) { allRoutes.splice(i, 1); }
    }
    if (!api.s.voucher) {
      i = allRoutes.indexOf(voucher);
      if (i >= 0) { allRoutes.splice(i, 1); }
    }

  }
  const r = getTreeElMenuData(await setPer() as any);
  return r;
}
export default router;
